<?php
/**
 * 金融控制器
 */
namespace app\index\controller;
use app\common\exception\System as SystemException;
use Exception;

class Finance extends Common
{
  /**
   * 不需要登录
   * @var array
   */
  protected $noNeedLogin = ['*'];

  /**
   * 金融主页
   */
  public function index()
  {
    $res = service('index/Finance')->index();
    return $this->successReturn($res,SystemException::E_SUCCESS,'获取成功');
  }
  //申请贷款前判断用户是否有正在申请中的贷款及申请状态
  public function status()
  {
    $member_id=$this->auth->member_id;
    $model = new \app\index\model\Apply;
    $res=$model->where('member_id',$member_id)->order("update_time desc")->limit(1)->select();
    if(empty($res->toArray())){
      $arr=['status'=>4,'msg'=>'未申请'];  
    }else{
      if($res[0]['is_verfiy']==1){
        $arr=['status'=>1,'msg'=>'待审核'];
      }else if($res[0]['is_verfiy']==2){
          $arr=['status'=>2,'msg'=>'已通过'];
      }else{
          $arr=['status'=>3,'msg'=>'已拒绝'];
      }
    }
    return $this->successReturn($arr,SystemException::E_SUCCESS,$arr['msg']);
  }

  //获取个人贷款情况
  public function details()
  {
  	$param = $this->request->param('id');
    $res = service('index/Finance')->getFininfo($param);
    return $this->successReturn($res,SystemException::E_SUCCESS,'获取成功');
  }

  //借贷申请
  public function add()
  {
    $data=$this->request->param();
    $data['member_id']=$this->auth->member_id;
    $model = new \app\index\model\Apply;
    $res=$model->allowField(true)->save($data);
    if($res){
      $res=['status'=>1,'msg'=>'提交成功'];
      return $this->successReturn($data,SystemException::E_SUCCESS, $res['msg']); 
    }else{
      $res=['status'=>0,'msg'=>'提交失败'];
      return $this->successReturn($res,SystemException::E_SUCCESS, $res['msg']);      
    }
  }
}